home *** CD-ROM | disk | FTP | other *** search
/ The X-Philes (2nd Revision) / The X-Philes Number 1 (1995).iso / xphiles / hp48_2 / areuh.tar / areuh / assembler / autil.c < prev    next >
C/C++ Source or Header  |  1990-10-10  |  6KB  |  266 lines

  1. /*
  2.  * Authors :
  3.  *   Pierre DAVID (pda@masi.ibp.fr or pda@frunip62.bitnet)
  4.  *   Janick TAILLANDIER
  5.  *
  6.  * This program can be freely used or distributed as long as this
  7.  * note is kept.
  8.  *
  9.  * This program is provided "as is".
  10.  */
  11.  
  12. /******************************************************************************
  13.  
  14.                 TITAN ASSEMBLER
  15.  
  16.                    GENERAL UTILITIES
  17.  
  18.  
  19. hard_init, soft_init, between, term, memoire
  20.  
  21. ******************************************************************************/
  22.  
  23.  
  24. #include "aglobal.h"
  25.  
  26. void free_mem () ;
  27.  
  28. extern void o_init() ;
  29. extern void dump_linker_infos () ;
  30. extern void l_init(), l_flush() ;
  31. extern void s_init() ;
  32. extern void i_tab0(), i_tab1(), i_tab2(), i_tab3(), i_tab4(), i_tab5() ;
  33. extern void print_ref() ;
  34.  
  35. extern char *malloc () ;
  36.  
  37. /******************************************************************************
  38.  
  39.                    INIT
  40.  
  41.  
  42. synopsis : void init()
  43. description : initiates the pass one. Opens source file, initializes the symbol
  44.           list. Called for each file assembled.
  45.  
  46. ******************************************************************************/
  47.  
  48. void init ()
  49. {
  50.     uchar file [MAXLEN+1], *psource, *pfile ;
  51.  
  52.     if (!try_source (fsource))         /* if can't open succesfully */
  53.     {
  54.     pfile = file ; psource = fsource ;
  55.     while ((*pfile = *psource) && (*pfile != '.'))
  56.         {
  57.         pfile++ ;
  58.             psource++ ;
  59.         }
  60.     if (*pfile == EOL)
  61.     {
  62.         strcpy (pfile, ".as") ;
  63.         if (try_source (file)) strcpy (fsource, file) ;
  64.     }
  65.     }
  66.     if (fd_s==NULL) error (ERROPN, fsource) ;
  67.  
  68.     s_init() ;                                  /* symbol table init */
  69.  
  70.     passnb = 1 ;
  71.     linenb = 0 ;
  72.     cntlist = 0 ;
  73.     modular = 1 ;
  74.     *l_title = *l_stitle = EOL ;
  75. }
  76.  
  77.  
  78. /******************************************************************************
  79.  
  80.                     BETWEEN
  81.  
  82.  
  83. synopsis : void between()
  84. description : terminates the passe one, and prepares pass two.
  85.           Restore file source pointer, opens object file.
  86.  
  87. ******************************************************************************/
  88.  
  89. void between()
  90. {
  91.     int r ;
  92.  
  93.     r =  fseek (fd_s,0L,0);
  94.     if (r)  error (ERRREW, fsource) ;
  95.     o_init() ;
  96.     cntlist = cntlist_ref ;
  97.     l_init() ;
  98.     linenb = 0 ;
  99.     passnb = 2 ;
  100.     print_this_line = 1 ;
  101.     errcnt = 0 ;
  102.     headxu = (struct xused *) NULL ;
  103. }
  104.  
  105. /******************************************************************************
  106.  
  107.                      TERM
  108.  
  109.  
  110. synopsis : void term ()
  111. description : ends the pass two, and thus the assembly.
  112.           Closes source file, and flushes object files.
  113.  
  114. ******************************************************************************/
  115.  
  116. void term ()
  117. {
  118.     if (fclose (fd_s)) error (ERRCLO, fsource) ;
  119.     if (modular) dump_linker_infos () ;
  120.     if (fclose (fd_o)) error (ERRCLO, fobject) ;
  121.     print_ref () ;
  122.     l_flush () ;
  123.  
  124.     free_mem () ;
  125. }
  126.  
  127.  
  128. int try_source (file)
  129. uchar *file ;
  130. {
  131.     long int magic ;
  132.  
  133.     fd_s = fopen (file, "r") ;
  134.     if (fd_s)
  135.     {
  136.     fread (&magic, sizeof (long int), 1, fd_s) ;
  137.     if (((magic>=ALF_MAGIC)&&(magic<=AL_MAGIC))||
  138.         ((magic>=AOF_MAGIC)&&(magic<=AO_MAGIC)))
  139.     {
  140.         fclose (fd_s) ;
  141.         fd_s = (FILE *) NULL ;
  142.     }
  143.     else fseek (fd_s, 0L, 0) ;
  144.     }
  145.     return ((int) fd_s) ;
  146. }
  147.  
  148.  
  149. /******************************************************************************
  150.  
  151.                   MEMOIRE
  152.  
  153.  
  154. synopsis : uchar *memoire (size)
  155.        int size
  156. description : get memory from heap using malloc. It is just a layer above
  157.           malloc, including a test.
  158.  
  159. ******************************************************************************/
  160.  
  161. uchar *memoire (size)
  162. int size ;
  163. {
  164.     uchar *x ;
  165.  
  166.     if ((x = (uchar *) malloc (size)) == NULL)
  167.     error (ERRMEM, "") ;
  168.     return (x) ;
  169. }
  170.  
  171.  
  172. /******************************************************************************
  173.  
  174.                     UPRC
  175.  
  176.  
  177. synopsis : void uprc (str)
  178.        uchar *str
  179. description : uppercase a string
  180.  
  181. ******************************************************************************/
  182.  
  183. void uprc (str)
  184. uchar *str ;
  185. {
  186.     while (*str)
  187.     {
  188.     if ((*str >= 'a') && (*str <= 'z'))
  189.         *str -= 32 ;
  190.     str++ ;
  191.     }
  192. }
  193.  
  194.  
  195. /******************************************************************************
  196.  
  197.                  FORMAT_HEX
  198.  
  199.  
  200. synopsis : void format_hex (str, val, dig)
  201.        uchar *str
  202.            saddr val
  203.            int dig
  204. description : stores into str the hexadecimal string representing the dig
  205.               low order hex digits of val.
  206.  
  207. ******************************************************************************/
  208.  
  209. void format_hex (str, val, dig)
  210. uchar *str ;
  211. saddr val ;
  212. int dig ;
  213. {
  214.     register int i, h ;
  215.  
  216.     for (i=dig-1; i>=0; i--)
  217.     {
  218.     h = (int) (val & ((saddr) 0xf)) ;
  219.     str [i] = h + ((h < 10) ? '0' : 'A' - 10) ;
  220.     val >>= 4 ;
  221.     }
  222.     str [dig] = EOL ;
  223. }
  224.  
  225.  
  226. /******************************************************************************
  227.  
  228.                  FREE_MEM
  229.  
  230.  
  231. synopsis : void free_mem ()
  232. description : frees all dynamically allocated memory during both passes.
  233.  
  234. ******************************************************************************/
  235.  
  236. void free_mem ()
  237. {
  238.     struct xused *u ;
  239.     struct symbol *s1, *s2 ;
  240.     struct xtable *x1, *x2 ;
  241.     int i ;
  242.  
  243.     for (i=0; i<=255; i++)
  244.     {
  245.         s1 = h_label[i]->s_next ;
  246.         while (s1)
  247.         {
  248.             if (xref)
  249.             {
  250.                 x1 = s1->s_xref ;
  251.                 while (x1)
  252.                 {
  253.                     x2 = x1->x_next ;
  254.                     free ((char *) x1) ;
  255.                     x1 = x2 ;
  256.                 }
  257.             }
  258.             if (s1->s_def)  free ((char *) s1->s_def) ;
  259.             s2 = s1->s_next ;
  260.             free ((char *) s1) ;
  261.             s1 = s2 ;
  262.         }
  263.     }
  264.     free ((char *) h_label [0]) ;
  265. }
  266.